package test.swing;

/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @date 2023-8-30
 */
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class DataFormToImg {

    public static void main(String[] args) throws Exception {
        Object[][] data = { { "VIE架构企业_2019-01-01_2019-12-31_销售成本年度平均", new Double(4663.5), new Double(10) } };

        String[] columns = { "分年统计销售成本平均值_主体名称", "分年统计销售成本平均值_营业成本平均值", "分年统计销售成本平均值_主体个数" };

        JTable table = new JTable(data, columns);
        JScrollPane scroll = new JScrollPane(table);

        JFrame f = new JFrame();
        f.setContentPane(scroll);
        f.pack();

        int x = (int) table.getTableHeader().getSize().getWidth();
        int y = (int) table.getTableHeader().getSize().getHeight() + (int) table.getSize().getHeight();

        BufferedImage bi = new BufferedImage((int) x, (int) y, BufferedImage.TYPE_INT_RGB);

        Graphics g = bi.createGraphics();
        table.getTableHeader().paint(g);
        g.translate(0, table.getTableHeader().getHeight());
        table.paint(g);
        g.dispose();

        JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(bi)));
        ImageIO.write(bi, "png", new File("d:/poi/table.png"));

        System.exit(0);
    }
}
